/*
 * @Author: wifipx 
 * @Date: 2021-03-29 17:09:02
 * @LastEditTime: 2021-03-29 17:10:13
 * @LastEditors: 笨小孩  <hope.deng>
 * @Description: 文件下载
 * @FilePath: /wifipix-public/code-snippet/downloadFile.js
 * 佛祖保佑 永不宕机 永无BUG
 */


// 此方法不会触发跳转
function downloadFile (url, fileName) {
  // Get 方式，post 方式要注意修改相关参数
  // url 服务端应允许跨域
  var request = new XMLHttpRequest();
  request.responseType = "blob";
  request.open("GET", url);
  request.onload = function () {
    var url = window.URL.createObjectURL(this.response);
    var a = document.createElement("a");
    document.body.appendChild(a);
    a.href = url;
    a.download = fileName
    a.click();
  }
  request.send();
}